﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using FriendTest.Models;
using FriendTest.FriendService;

namespace FriendTest.Controllers
{

    [HandleError]
    public class AccountController : Controller
    {

        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        // **************************************
        // URL: /Account/LogOn
        // **************************************

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(MyLogOnModel model, string returnUrl)
        {
            //if (ModelState.IsValid)
            //{
            Service1 myFriendService = new Service1();
            int userId = myFriendService.Logon(model.userName, model.passWord);
                if (userId != -1)
                {
                    //FormsService.SignIn(model.UserName, model.RememberMe);
                    UserData user = myFriendService.GetUserData(userId);
                    FormsAuthentication.SetAuthCookie(user.userName+","+user.id.ToString(), model.rememberMe);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "提供的用户名或密码不正确。");
                }
            //}

            // 如果我们进行到这一步时某个地方出错，则重新显示表单
            return View(model);
        }

        // **************************************
        // URL: /Account/LogOff
        // **************************************

        public ActionResult LogOff()
        {
            FormsService.SignOut();

            return RedirectToAction("Index", "Home");
        }

        // **************************************
        // URL: /Account/Register
        // **************************************

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(MyRegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // 尝试注册用户
                Service1 myFriendService = new Service1();

                int userId = myFriendService.RegistereUser(model.emailAddress,model.userName,model.passWord,0);
                
                if (userId != -1)
                {
                    FormsAuthentication.SetAuthCookie(model.userName + "," + userId,false);

                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("","注册失败");
                }
            }

            // 如果我们进行到这一步时某个地方出错，则重新显示表单
            return View(model);
        }
    }
}
